package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQuerySpaceListReq;
import com.sg.dto.base.framework.req.QuerySpaceDetailReq;
import com.sg.dto.base.framework.req.QuerySpaceListReq;
import com.sg.service.base.framework.MSpaceService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.SpaceMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MSpaceServiceImpl
        extends ServiceImpl<SpaceMapper, Space>
        implements MSpaceService {

    @Autowired
    SpaceMapper spaceMapper;

    /**
     * 1-2-04查询空间详情[161]
     * gen by moon at 3/31/2023, 3:24:01 AM
     */
    @Trace(operationName = "1-2-04查询空间详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Space querySpaceDetail(QuerySpaceDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<Space> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<Space> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                Space retData = retList.get(0);
                retData.setSpaceLogo(CommonFunctionHelper.getFilePath(retData.getSpaceLogo()));//otherModel: ALL_PATH
                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-04查询空间列表[60]
     * gen by moon at 10/23/2022, 2:25:38 PM
     */
    @Trace(operationName = "1-2-04查询空间列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Space> querySpaceList(QuerySpaceListReq reqDto) {

        try {
            QueryWrapper<Space> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Space.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Space.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Space.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<Space>) super.list(queryWrapper)).stream().map(item -> {
                item.setSpaceLogo(CommonFunctionHelper.getFilePath(item.getSpaceLogo()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-04批量查询空间列表[2151]
     * gen by moon at 10/23/2022, 2:25:39 PM
     */
    @Trace(operationName = "1-2-04批量查询空间列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Space> batchQuerySpaceList(BatchQuerySpaceListReq reqDto) {

        try {
            QueryWrapper<Space> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getSpaceList() == null || reqDto.getSpaceList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("space_id", reqDto.getSpaceList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Space.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Space.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Space.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<Space>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-04删除空间[59]
     * gen by moon at 10/23/2022, 2:25:38 PM
     */
    @Trace(operationName = "1-2-04删除空间")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFrameworkSpace(String spaceId) {

        return super.removeById(spaceId);
    }

    /**
     * 1-2-04修改空间[58]
     * gen by moon at 10/23/2022, 2:25:37 PM
     */
    @Trace(operationName = "1-2-04修改空间")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSpace(Space space) {
        // TODO: 2024/1/19
//     if(!StringUtils.isEmpty(space.getSpaceLogo())){
//         space.setSpaceLogo(CommonFunctionHelper.replaceFilePath(space.getSpaceLogo()));
//     }
        space.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(space);
    }

    /**
     * 1-2-04新增空间[57]
     * gen by moon at 10/23/2022, 2:25:36 PM
     */
    @Trace(operationName = "1-2-04新增空间")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addSpace(Space space) {
        // TODO: 2024/1/19
//     if(!StringUtils.isEmpty(space.getSpaceLogo())){
//         space.setSpaceLogo(CommonFunctionHelper.replaceFilePath(space.getSpaceLogo()));
//     }
        space.setSpaceId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        space.setSpaceCode("1001");//CUSTOM_CONVENTION
        space.setIsInvalid("FALSE");
        space.setIsValid("FALSE");
        space.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        space.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        space.setAscriptionSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        space.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        space.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (super.save(space)) {
            return space.getSpaceId();
        } else {
            return "-1";
        }

    }

    /**
     * M1删除空间测试[4873]
     * gen by moon at 10/23/2022, 2:25:40 PM
     */
    @Trace(operationName = "M1删除空间测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSpaceTestData(String spaceId) {

        return super.removeById(spaceId);
    }
}
